﻿Imports System                  '*
Imports System.Drawing          '*  
Imports System.Collections      '*
Imports System.ComponentModel   '*
Imports System.Windows.Forms    '*-From Microsoft .NET Framework 4.5
Imports System.Data             '*
Imports System.Threading        '*
Imports System.Configuration    '*
Imports System.Text             '*

Imports Netron.Lithium          '*
Imports OpenNLP.Tools           '*- From SharpNLP and OpenNLP
Imports OpenNLP.Tools.Parser    '*
Imports OpenNLP.Tools.Util      '*


Public Class clsLingo
    Private lithiumControl As New Netron.Lithium.LithiumControl
    Private mModelPath As String
    Private mParser As EnglishTreebankParser
    Private mParse As Parse

    Private XMLTree As String = ""
    Private mSentenceDetector As SentenceDetect.MaximumEntropySentenceDetector
    Private mTokenizer As Tokenize.EnglishMaximumEntropyTokenizer
    Private mPosTagger As PosTagger.EnglishMaximumEntropyPosTagger
    Private mChunker As Chunker.EnglishTreebankChunker

    Private mNameFinder As NameFind.EnglishNameFinder
    Private mCoreferenceFinder As Lang.English.TreebankLinker
    Private objXML As New clsXMLCore

    Public Property SetModelPath() As String
        Get
            Return mModelPath
        End Get
        Set(ByVal value As String)
            mModelPath = value
        End Set
    End Property

    Public Function POSTag(ByVal Text As String) As String
        Dim output As StringBuilder = New StringBuilder()
        Dim sentences() As String = SplitSentences(Text)

        For Each sentence As String In sentences
            Dim tokens() As String = TokenizeSentence(sentence)
            Dim tags() As String = PosTagTokens(tokens)

            For currentTag As Integer = 0 To (tags.Length - 1)
                output.Append(tokens(currentTag)).Append("/").Append(tags(currentTag)).Append(" ")
                output.Append(vbCrLf)
            Next currentTag
        Next
        Return output.ToString()
    End Function

    Private Function TokenizeSentence(ByVal sentence As String) As String()
        mTokenizer = New OpenNLP.Tools.Tokenize.EnglishMaximumEntropyTokenizer(mModelPath + "EnglishTok.nbin")
        Return mTokenizer.Tokenize(sentence)
    End Function

    Private Function PosTagTokens(ByRef tokens() As String) As String()
        mPosTagger = New OpenNLP.Tools.PosTagger.EnglishMaximumEntropyPosTagger(mModelPath + "EnglishPOS.nbin", mModelPath + "\Parser\tagdict")
        Return mPosTagger.Tag(tokens)
    End Function

    Private Function SplitSentences(ByVal paragraph As String) As String()
        mSentenceDetector = New SentenceDetect.EnglishMaximumEntropySentenceDetector(mModelPath + "EnglishSD.nbin")
        Return mSentenceDetector.SentenceDetect(paragraph)
    End Function

    Public Function ShowParseTree(ByVal Text As String) As String
        Dim output As StringBuilder = New StringBuilder()
        Dim sentences() As String = SplitSentences(Text)
        For Each sentence As String In sentences
            output.Append(ParseSentence(sentence).Show()).Append(vbCrLf)
        Next
        Return output.ToString()
    End Function

    Public Function ShowParseXML(ByVal Text As String) As String
        Dim output As StringBuilder = New StringBuilder()
        Dim sentences() As String = SplitSentences(Text)
        For Each sentence As String In sentences
            output.Append(ParseSentence(sentence).Show()).Append(vbCrLf)
        Next
        XMLTree = objXML.GetXMLTree(output.ToString())
        Return XMLTree
    End Function

    Private Function ParseSentence(ByVal sentence As String) As OpenNLP.Tools.Parser.Parse
        mParser = New OpenNLP.Tools.Parser.EnglishTreebankParser(mModelPath, True, False)
        Return mParser.DoParse(sentence)
    End Function
End Class
